const md5 = require("../../utils/md5.js");
const innerAudioContext = wx.createInnerAudioContext();
innerAudioContext.onPlay(() => {
  //console.log('开始播放')
})
innerAudioContext.onError((res) => {
  //console.log(res.errMsg)
  //console.log(res.errCode)
})
const app = getApp()
Page({
  data: {
    readtext: '读 音',
    isDisabledRead: false,
    apiUrl: app.globalData.url + '/translate',
    decArray: ['自动识别', '中文', '日文', '英文', '韩文', '法文', '俄文', '葡萄牙文', '西班牙文', '越南文'],
    valueArray: ['auto', 'zh-CHS', 'ja', 'EN', 'ko', 'fr', 'ru', 'pt', 'es', 'vi'],
    fromIndex: 0,
    toIndex: 1,
    tSpeakUrl: '',
    speakUrl: '',
    result: ''
  },
  cleartext: function(e) { //点击按钮清除文字      
    this.setData({
      inputstr: '',
      result: ''
    })
  },
  copytext: function(e) { //点击按钮清除文字     
    wx.setClipboardData({ //剪贴板粘贴
      data: this.data.result,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            //console.log(res.data) // data
          }
        })
      }
    });
  },
  readtext: function() {
    if (this.data.tSpeakUrl == '' || this.data.tSpeakUrl == null) return;
    else {
      // innerAudioContext.play()
      let disableTime = 4;
      this.setData({
        isDisabledRead: true,
        readtext: "缺钱(" + 5 + "秒)"
      })
      let setReadText = (disableTime, isDisabledRead) => {
        if (isDisabledRead) {
          this.setData({
            isDisabledRead: isDisabledRead,
            readtext: "缺钱(" + disableTime + "秒)"
          })
        } else {
          this.setData({
            isDisabledRead: isDisabledRead,
            readtext: '读音'
          })
        }
      }
      let interval = setInterval(function() {
        if (disableTime <= 0) {
          setReadText(disableTime, false);
          clearInterval(interval);
          disableTime = 4;
        } else {
          setReadText(disableTime, true);
          disableTime--;
        }
      }, 1000);
    }
  },
  bindPickerChangeFrom: function(e) {
    this.setData({
      fromIndex: e.detail.value
    })
  },
  bindPickerChangeTo: function(e) {
    this.setData({
      toIndex: e.detail.value
    })
  },
  bindFormSubmit: function(e) {
    let queryTmp = e.detail.value.textarea;
    if (queryTmp.trim() == '') return;
    let setResult = resultTemp => {
      this.setData({
        result: resultTemp
      })
    };
    let setAudioSrc = srcTemp => {
      innerAudioContext.src = srcTemp;
      this.setData({
        tSpeakUrl: srcTemp
      })
    };
    wx.request({
      url: this.data.apiUrl,
      header: {
        'charset': 'utf-8',
        'content-type': 'application/json' // 默认值
      },
      data: {
        'q': queryTmp,
        'fromLang': this.data.valueArray[this.data.fromIndex],
        'toLang': this.data.valueArray[this.data.toIndex]
      },
      method: 'POST',
      success: function(res) {
        //console.log(res);
        if (res.statusCode != 200) return;
        if (res.data.errorCode != '0') {
          setResult(res.data.errorCode)
        } else {
          setAudioSrc(res.data.tSpeakUrl);
          if (res.data.basic.explains != null) {
            let basic = ''
            if (res.data.basic.exam_type != '') {
              basic += '考点：' + res.data.basic.exam_type + '\n'
            }
            if (res.data.basic.us_phonetic != '') {
              basic += "美音：【" + res.data.basic.us_phonetic + '】\n'
            }
            if (res.data.basic.uk_phonetic != '') {
              basic += "英音：【" + res.data.basic.uk_phonetic + '】\n'
            }
            if (res.data.basic.explains != '') {
              basic += "释义：" + res.data.basic.explains + '\n'
            }
            setResult(basic);
          } else {
            setResult(res.data.translation[0])
          }
        }
      },
      fail: function(e) {
        innerAudioContext.src = '';
        console.log(e);
      }
    })
  },
  onLoad: function() {

  },
  onShareAppMessage: function() { //用户点击右上角分享   
    return {
      title: '篮子翻译分享', // 分享标题
      desc: '翻译页面分享', // 分享描述
      path: 'pages/translate/translate' // 分享路径
    }
  },
})